using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace WebApplication8.Data
{
    public partial class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
        {

        }
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);

            RunSeed(builder);

        }

        private void RunSeed(ModelBuilder builder)
        {
            // any guid
            const string ADMIN_ID = "a18be9c0-aa65-4af8-bd17-00bd9344e575";
            const string STUDENT_ID = "a18be9c0-aa65-4af8-bd17-00bd9344e515";
            // any guid, but nothing is against to use the same one
            const string ROLE_ID = ADMIN_ID;
            builder.Entity<IdentityRole>().HasData(new IdentityRole
            {
                Id = ROLE_ID,
                Name = "admin",
                NormalizedName = "admin"
            });

            var hasher = new PasswordHasher<ApplicationUser>();
            var user = new ApplicationUser
            {
                Id = ADMIN_ID,
                UserName = "admin",
                NormalizedUserName = "admin",
                Email = "some-admin-email@nonce.fake",
                NormalizedEmail = "some-admin-email@nonce.fake",
                EmailConfirmed = true,
                PasswordHash = hasher.HashPassword(null, "123456"),
                SecurityStamp = string.Empty
            };
            var userstudent = new ApplicationUser
            {
                Id = STUDENT_ID,
                UserName = "student",
                NormalizedUserName = "student",
                Email = "some-student-email@nonce.fake",
                NormalizedEmail = "some-student-email@nonce.fake",
                EmailConfirmed = true,
                PasswordHash = hasher.HashPassword(null, "123456"),
                SecurityStamp = string.Empty
            };
            builder.Entity<ApplicationUser>().HasData(user, userstudent);

            builder.Entity<IdentityUserRole<string>>().HasData(new IdentityUserRole<string>
            {
                RoleId = ROLE_ID,
                UserId = ADMIN_ID
            });
            var student = new Student
            {
                Id = STUDENT_ID,
                UserId = STUDENT_ID
            };
            builder.Entity<Student>().HasData(student);
            var teacher = new Teacher
            {
                Id = ADMIN_ID,
                UserId = ADMIN_ID
            };
            builder.Entity<Teacher>().HasData(teacher);

            builder.Entity<Course>().HasData(new Course
            {
                Id = ADMIN_ID,
                TeacherId = ADMIN_ID,
                CourseName = "管理信息系统",
                XueNianXueQi = "2018-2019(2)"
            });
        }

        //用户
        public DbSet<Student> Student { get; set; }
        public DbSet<Teacher> Teacher { get; set; }
        //course
        public DbSet<Course> Course { get; set; }


        //班级
        public DbSet<BanJi> BanJi { get; set; }
        public DbSet<BanJiStudent> BanJiStudent { get; set; }


        //作业
        public DbSet<Question> Question { get; set; }
        public DbSet<Homework> Homework { get; set; }
        public DbSet<HomeworkDtl> HomeworkDtl { get; set; }
        public DbSet<StudentHomework> StudentHomework { get; set; }
        public DbSet<StudentHomeworkDtl> StudentHomeworkDtl { get; set; }

        //上课
        public DbSet<Lesson> Lesson { get; set; }
        public DbSet<Danmaku> Danmaku { get; set; }
        public DbSet<LessonStudent> LessonStudent { get; set; }


    }
}
